package org.mspring.mlog.freemarker.directive;

import freemarker.core.Environment;
import freemarker.template.*;
import org.mspring.mlog.entity.Tag;
import org.mspring.mlog.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 标签列表
 *
 * @author Gao Youbo
 * @since 2015-01-24 12:44
 */
@Component
public class TagListDirectiveModel implements TemplateDirectiveModel {

    private TagService tagService;

    @Override
    public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
        List<Tag> tagList = tagService.listAll();
        env.setVariable("tagList", ObjectWrapper.DEFAULT_WRAPPER.wrap(tagList));
        body.render(env.getOut());
    }

    @Autowired
    public void setTagService(TagService tagService) {
        this.tagService = tagService;
    }
}
